package com.koron.order.patrol.service.impl;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.patrol.bean.po.PatrolEquipmentPo;
import com.koron.order.patrol.bean.po.PatrolPointPo;
import com.koron.order.patrol.mapper.PatrolPointMapper;
import com.koron.order.patrol.service.PatrolEquipmentService;
import com.koron.order.patrol.service.PatrolPointService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 巡视工单-巡视点从表(PatrolPoint)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-26 15:27:37
 */
@Service("patrolPointService")
public class PatrolPointServiceImpl implements PatrolPointService {

    @Autowired
    private PatrolEquipmentService patrolEquipmentService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, PatrolPointPo patrolPoint) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.insertOrUpdate(patrolPoint);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<PatrolPointPo> patrolPointList) {

        Assert.notEmpty(patrolPointList, "巡视点不能为空");

        List<String> patrolIds = patrolPointList.stream().map(PatrolPointPo::getPatrolId).distinct().collect(Collectors.toList());

        Assert.notEmpty(patrolIds, "巡视工单id不能为空");

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        //先根据 巡视工单ids删除
        mapper.deleteByPatrolIds(patrolIds);

        return mapper.insertBatch(patrolPointList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, PatrolPointPo patrolPoint) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.insert(patrolPoint);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<PatrolPointPo> patrolPointList) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.insertBatch(patrolPointList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, PatrolPointPo patrolPoint) {
        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);
        return mapper.update(patrolPoint);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public PatrolPointPo queryById(SessionFactory factory, String id) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByIds")
    public List<PatrolPointPo> queryByIds(SessionFactory factory, List<String> ids) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        return mapper.queryByIds(ids);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<PatrolPointPo> queryList(SessionFactory factory, PatrolPointPo patrolPoint) {

        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        List<PatrolPointPo> list = mapper.queryList(patrolPoint);
        if (CollUtil.isEmpty(list)) {
            return list;
        }
        // 填充 是否异常result
        List<String> patrolPointPos = CollStreamUtil.toList(list, PatrolPointPo::getId);
        List<PatrolEquipmentPo> patrolEquipmentPos = patrolEquipmentService.queryByIds(factory, patrolPointPos);
        Map<String, List<PatrolEquipmentPo>> patrolPointPoListMap =
                CollStreamUtil.groupByKey(patrolEquipmentPos, PatrolEquipmentPo::getPatrolPointId);
        if (CollUtil.isEmpty(patrolPointPoListMap)) {
            return list;
        }
        for (PatrolPointPo patrolPointPo : list) {
            List<PatrolEquipmentPo> patrolEquipmentPos1 = patrolPointPoListMap.get(patrolPointPo.getId());
            if (CollUtil.isEmpty(patrolEquipmentPos1)) {
                continue;
            }
            boolean b = patrolEquipmentPos1.stream().anyMatch(e -> Optional.ofNullable(e.getResult()).orElse(0) == -1);
            patrolPointPo.setResult(b ? -1 : 1);
        }
        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, PatrolPointPo patrolPoint, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        PatrolPointMapper mapper = factory.getMapper(PatrolPointMapper.class);

        List<PatrolPointPo> list = mapper.queryList(patrolPoint);

        return new PageInfo(list);
    }

}